package com.zoffy.wmsbackend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zoffy.wmsbackend.model.entity.*;
import com.zoffy.wmsbackend.service.*;
import jakarta.annotation.Resource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

@RestController
public class DataGenerate {
    @Resource
    private StockInOrderService stockInOrderService;
    @Resource
    private StockInOrderDetailService stockInOrderDetailService;
    @Resource
    private StockOutOrderService stockOutOrderService;
    @Resource
    private StockOutOrderDetailService stockOutOrderDetailService;
    @Resource
    private WarehouseService warehouseService;
    @Resource
    private StockService stockService;
    @Resource
    private GoodsService goodsService;
    @Resource
    private MaterialUsagePlanService materialUsagePlanService;
    @Resource
    private MaterialUsagePlanDetailService materialUsagePlanDetailService;
    @Resource
    private ReportController reportController;
    @Resource
    private CostController costController;

    @Scheduled(cron = "0 0 4 * * ?")
    @GetMapping("/data/generate/zoffy")
    public void generate() {
        List<Warehouse> warehouseList = warehouseService.list();
        List<Goods> goodsList = goodsService.list();
        Random random = new Random();
        for (Warehouse warehouse : warehouseList) {

            for (Goods goods : goodsList) {
                QueryWrapper<Stock> wrapper = new QueryWrapper<>();
                Stock stock = stockService.getOne(wrapper.eq("warehouse_id", warehouse.getId()).eq("goods_id", goods.getId()));
                if (stock == null) {
                    stock = new Stock();
                    stock.setWarehouseId(warehouse.getId());
                    stock.setGoodsId(goods.getId());
                    stock.setQuantity(0);
                }
                int count= random.nextInt(5);
                for (int i = 0; i < count; i++) {
                    //入库单创建
                    StockInOrder stockInOrder = new StockInOrder();
                    stockInOrder.setStockInNo(UUID.randomUUID().toString().replace("-", ""));
                    stockInOrder.setWarehouseId(warehouse.getId());
                    stockInOrder.setStatus(90);
                    StockInOrderDetail stockInOrderDetail = new StockInOrderDetail();
                    stockInOrderService.save(stockInOrder);

                    stockInOrderDetail.setStockInOrderId(stockInOrder.getId());
                    stockInOrderDetail.setStockInDetailNo(stockInOrder.getStockInNo() + "-1");
                    stockInOrderDetail.setWarehouseId(warehouse.getId());
                    stockInOrderDetail.setGoodsId(goods.getId());
                    int num = random.nextInt(120);
                    stockInOrderDetail.setQuantity(num);
                    stockInOrderDetail.setStatus(1);
                    stockInOrderDetailService.save(stockInOrderDetail);
                    stock.setQuantity(stock.getQuantity() + num);


                    //出库单创建
                    StockOutOrder stockOutOrder = new StockOutOrder();
                    stockOutOrder.setStockOutNo(UUID.randomUUID().toString().replace("-", ""));
                    stockOutOrder.setWarehouseId(warehouse.getId());
                    stockOutOrder.setStatus(i % 3 < 2 ? 90 : 0);
                    stockOutOrderService.save(stockOutOrder);

                    StockOutOrderDetail stockOutOrderDetail = new StockOutOrderDetail();
                    stockOutOrderDetail.setStockOutOrderId(stockOutOrder.getId());
                    stockOutOrderDetail.setStockOutOrderId(stockOutOrder.getId());
                    stockOutOrderDetail.setGoodsId(goods.getId());
                    stockOutOrderDetail.setWarehouseId(warehouse.getId());
                    stockOutOrderDetail.setStockOutDetailNo(stockOutOrder.getStockOutNo() + "-1");
                    num = Math.min(random.nextInt(1, 100), stock.getQuantity());
                    stockOutOrderDetail.setQuantity(num);
                    stockOutOrderDetail.setStatus(0);
                    stockOutOrderDetailService.save(stockOutOrderDetail);
                    stock.setQuantity(stock.getQuantity() - num);

                    stockService.saveOrUpdate(stock);
                }

            }
        }
        for (Warehouse warehouse : warehouseList) {

            for (Goods goods : goodsList) {
                //入库单创建
                StockInOrder stockInOrder = new StockInOrder();
                stockInOrder.setStockInNo(UUID.randomUUID().toString().replace("-", ""));
                stockInOrder.setWarehouseId(warehouse.getId());
                stockInOrder.setStatus(0);
                StockInOrderDetail stockInOrderDetail = new StockInOrderDetail();
                stockInOrderService.save(stockInOrder);

                stockInOrderDetail.setStockInOrderId(stockInOrder.getId());
                stockInOrderDetail.setStockInDetailNo(stockInOrder.getStockInNo() + "-1");
                stockInOrderDetail.setWarehouseId(warehouse.getId());
                stockInOrderDetail.setGoodsId(goods.getId());
                int num = random.nextInt(1, 120);
                stockInOrderDetail.setQuantity(num);
                stockInOrderDetail.setStatus(0);
                stockInOrderDetailService.save(stockInOrderDetail);
            }
        }

        for (Warehouse warehouse : warehouseList) {

            MaterialUsagePlan materialUsagePlan = new MaterialUsagePlan();
            materialUsagePlan.setPlanNo(UUID.randomUUID().toString().replace("-", ""));
            materialUsagePlan.setWarehouseId(warehouse.getId());
            materialUsagePlan.setStatus(0);
            materialUsagePlanService.save(materialUsagePlan);

            List<MaterialUsagePlanDetail> materialUsagePlanDetailList = new ArrayList<>();

            int start = random.nextInt(goodsList.size());
            for (int i = 1; i <= 3; i++) {
                start %= goodsList.size();
                Goods goods = goodsList.get(start);
                MaterialUsagePlanDetail materialUsagePlanDetail = new MaterialUsagePlanDetail();
                materialUsagePlanDetail.setMaterialUsagePlanId(materialUsagePlan.getId());
                materialUsagePlanDetail.setPlanDetailNo(materialUsagePlan.getPlanNo() + "-" + i);
                materialUsagePlanDetail.setWarehouseId(warehouse.getId());
                materialUsagePlanDetail.setGoodsId(goods.getId());
                materialUsagePlanDetail.setQuantity(random.nextInt(1, 30));
                materialUsagePlanDetail.setStatus(0);
                start += 1;
                materialUsagePlanDetailList.add(materialUsagePlanDetail);
            }
            materialUsagePlanDetailService.saveBatch(materialUsagePlanDetailList);
        }
    }
    @GetMapping("/data/generate/chart/zoffy")
    public void generateChart() {
        reportController.calculateReportDaySummaryStockIn();
        reportController.calculateReportDaySummaryStockOut();
        costController.calculate();
    }
}
